package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.aop.annotation.LogAnnotation;
import com.company.project.common.utils.Func;
import com.company.project.entity.TZjhmglEntity;
import com.company.project.mapper.TMiddlenumMapper;
import com.company.project.service.HttpSessionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.entity.TMiddlenumEntity;
import com.company.project.service.TMiddlenumService;



/**
 * 
 *
 * @author wenbin
 * @email *****@mail.com
 * @date 2023-10-29 16:36:44
 */
@Controller
@RequestMapping("/")
public class TMiddlenumController {
    @Autowired
    private TMiddlenumService tMiddlenumService;
    @Autowired
    private HttpSessionService httpSessionService;

    @Autowired
    private TMiddlenumMapper tMiddlenumMapper;

    /**
    * 跳转到页面
    */
    @GetMapping("/index/tMiddlenum")
    public String tMiddlenum() {
        return "tmiddlenum/list";
        }


    @ApiOperation(value = "新增")
    @PostMapping("tMiddlenum/add")
    @RequiresPermissions("tMiddlenum:add")
    @LogAnnotation(title = "中间号", action = "新增")
    @ResponseBody
    public DataResult add(@RequestBody TMiddlenumEntity tMiddlenum){
        //获取当前操作人
        String userId = httpSessionService.getCurrentUserId();

        String middlenum = tMiddlenum.getMiddlenum();
        if (middlenum.indexOf(",") > 0) {
            String[] split = middlenum.split(",");
            for (String s : split) {
                Long integer = tMiddlenumMapper.selectCount(Wrappers.<TMiddlenumEntity>lambdaQuery()
                        .eq(TMiddlenumEntity::getMiddlenum, s));
                if (integer>0)  return DataResult.fail("中间号已存在，添加失败！");

                TMiddlenumEntity middlenumEntity = new TMiddlenumEntity();
                middlenumEntity.setSycs(tMiddlenum.getSycs());
                middlenumEntity.setMiddlenum(s);
                middlenumEntity.setCjr(userId);
                middlenumEntity.setMtczcs(tMiddlenum.getSycs());
                middlenumEntity.setRemark(tMiddlenum.getRemark());
                tMiddlenumService.save(middlenumEntity);
            }
        } else {
            Long integer = tMiddlenumMapper.selectCount(Wrappers.<TMiddlenumEntity>lambdaQuery()
                    .eq(TMiddlenumEntity::getMiddlenum, middlenum));
            if (integer>0)  return DataResult.fail("中间号已存在，添加失败！");

            tMiddlenum.setMtczcs(tMiddlenum.getSycs());
            tMiddlenum.setCjr(userId);
            tMiddlenumService.save(tMiddlenum);
        }
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("tMiddlenum/delete")
    @RequiresPermissions("tMiddlenum:delete")
    @LogAnnotation(title = "中间号", action = "删除")

    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        tMiddlenumService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("tMiddlenum/update")
    @LogAnnotation(title = "中间号", action = "更新")

    @RequiresPermissions("tMiddlenum:update")
    @ResponseBody
    public DataResult update(@RequestBody TMiddlenumEntity tMiddlenum){
//        tMiddlenumService.updateById(tMiddlenum);
        tMiddlenumService.updateEntity(tMiddlenum);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @LogAnnotation(title = "中间号", action = "查询分页数据")
    @PostMapping("tMiddlenum/listByPage")
    @RequiresPermissions("tMiddlenum:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody TMiddlenumEntity tMiddlenum){
        //获取当前操作人
        String userId = httpSessionService.getCurrentUserId();
        Page page = new Page(tMiddlenum.getPage(), tMiddlenum.getLimit());
        LambdaQueryWrapper<TMiddlenumEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        if(Func.isNotEmpty(tMiddlenum.getMiddlenum()))queryWrapper.eq(TMiddlenumEntity::getMiddlenum, tMiddlenum.getMiddlenum());
        if(Func.isNotEmpty(userId))queryWrapper.eq(TMiddlenumEntity::getCjr,userId);
        queryWrapper.orderByDesc(TMiddlenumEntity::getAddtime);
        IPage<TMiddlenumEntity> iPage = tMiddlenumService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }
    @ApiOperation(value = "获取中间号")
    @LogAnnotation(title = "中间号", action = "获取中间号")
    @PostMapping("tMiddlenum/dataInfo/{tseatsid}")
    @ResponseBody
    public DataResult dataInfo(@PathVariable("tseatsid") String tseatsid) {
        List<TMiddlenumEntity> zjhs = tMiddlenumService.getZjhByTseatId(tseatsid);
        return DataResult.success(zjhs);
    }
}
